function getUserList() {
    $.ajax({
        url: "http://www.woniu-news.com:8082/getUserList.php",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                let content = ""
                for (let item of resp.data) {
                    let method = item['status'] === 'true'? "禁用" : "启用"
                    content += `
        <tr>
            <td>${item['username']}</td>
            <td>${item['mail']}</td>
            <td>${item['role']}</td>
            <td>${item['register']}</td>
            <td>${item['status']}</td>
            <td class="actions">
                <a href="edit_user.html?uid=${item.id}" class="edit">编辑</a>
                <a href="javascript:void(0)" data-id="${item.id}" class="change-status">${method}</a>
                <a href="reset_password.html?uid=${item.id}" class="reset-password">重置密码</a>
            </td>
        </tr>`
                }
                $(".tbody").html(content);
            } else if (resp.code === 401) {
                alert(resp.message)
                window.location.href = "index.html"
            } else {
                alert(resp.message)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}

function changeUserStatus(uid) {
    $.ajax({
        url: "http://www.woniu-news.com:8082/changeUserStatus.php",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        data: {
            uid: uid
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                getUserList()
            } else {
                alert(resp.message)
            }
        },
        error: (resp) => {
            alert(resp.message)
        }
    })
}


getUserList()

// 监听用户状态切换事件
$(document).on("click", ".change-status", function () {
    let uid = $(this).attr("data-id")
    changeUserStatus(uid)
})
